using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace UFrame
{
    /// <summary>
    /// 常用的正则表达式
    /// </summary>
    public class RegexTool
    {
        public static Dictionary<ERegexVerify, string> dic = new Dictionary<ERegexVerify, string>();

        public enum ERegexVerify
        {

            IP,

            QQ,

            /// <summary>
            /// 中国邮政编码(中国邮政编码为6位数字)
            /// </summary>
            ChinaPostalCode,
            InternetURL,
            Iphone,
            Email,
            /// <summary>
            /// 中国身份证
            /// </summary>
            ChinaIdentity,
            /// <summary>
            /// 数字
            /// </summary>
            Number,
            /// <summary>
            /// 字母
            /// </summary>
            Letter24,
        }

        static RegexTool()
        {
            //初始化
            dic.Add(ERegexVerify.IP, @"\d+\.\d+\.\d+\.\d+");
            dic.Add(ERegexVerify.QQ, "[1-9][0-9]{4,}");
            dic.Add(ERegexVerify.ChinaPostalCode, @"[1-9]\d{5}(?!\d)");
            dic.Add(ERegexVerify.Iphone, @"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
            dic.Add(ERegexVerify.InternetURL, @"[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$");
            dic.Add(ERegexVerify.Email, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            dic.Add(ERegexVerify.ChinaIdentity, @"^\d{15}|\d{18}$");
            dic.Add(ERegexVerify.Number, @"^[0-9]*$");
            dic.Add(ERegexVerify.Letter24, @"^[A-Za-z]+$");
        }


        public static bool IsVerify(ERegexVerify type, string value)
        {
            return Regex.IsMatch(value, dic[type]);
        }


        public static bool IsChar(string value)
        {
            return Regex.IsMatch(value, @"^'s'");
        }

        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

        public static bool IsNumericInteger(string value)
        {
            return Regex.IsMatch(value, @"^\d+$");
        }

        public static bool IsNumericDecimal(string value)
        {
            return Regex.IsMatch(value, @"^\d+\.\d+$");
        }
        public static bool IsNumericFloat(string value)
        {
            return Regex.IsMatch(value, @"^\d+\.*\d*[fF]$");
        }
        public static bool IsNumericDouble(string value)
        {
            return Regex.IsMatch(value, @"^\d+\.*\d*[dD]$");
        }

        public static bool IsNumberCount(string value, int count)
        {
            return Regex.IsMatch(value, @"^\d{" + count + "}$");
        }

        /// <summary>
        /// 是否是汉字 ^[\u4e00-\u9fa5]{0,}$
        /// </summary>
        public static bool IsChineseChar(string value)
        {
            return Regex.IsMatch(value, @"^[\u4e00-\u9fa5]{0,}$");
        }


        /// <summary>
        /// 是否是英文数字 ^[A-Za-z0-9]+$
        /// </summary>
        public static bool IsEnglishNumber(string value)
        {

            return Regex.IsMatch(value, @"^[A-Za-z0-9]+$");
        }
    }
}